package com.higgins.client.agentapp.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.higgins.client.GreetingService;
import com.higgins.client.GreetingServiceAsync;
import com.higgins.client.agentapp.datamodel.AgencyDTO;
import com.higgins.client.agentapp.datamodel.AgentDTO;
import com.higgins.client.agentapp.datamodel.JobSiteDTO;
import com.higgins.client.agentapp.interfaces.AgencyListListener;
import com.higgins.client.agentapp.interfaces.AgentListListener;
import com.higgins.client.agentapp.interfaces.JobSiteListListener;
import com.higgins.client.dataModel.CoveringLetterDTO;
import com.higgins.client.dataModel.RecipientCountDTO;
import com.higgins.client.dataModel.StatusDTO;
import com.higgins.client.generic.components.GenericColumns;
import com.higgins.client.generic.components.GenericColumn;

public class AdvertisedJobColumns extends GenericColumns implements JobSiteListListener, AgencyListListener, AgentListListener	{
	private static final Logger log = Logger.getLogger(AdvertisedJobColumns.class.getName());
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private AdvertisedJobTable table;
	private boolean jobSiteListLoaded = false;
	private boolean agencyListLoaded = false;
	private boolean agentListLoaded = false;

	public AdvertisedJobColumns()	{
		super();
		initialiseColumns();
	}
	private void initialiseColumns()	{
		GenericColumn genericColumn = new GenericColumn();
		genericColumn.setTitle("ID");
		genericColumn.setIndex(0);
		genericColumn.setType(Integer.class);
		genericColumn.setWidth(25);
		genericColumn.setEditable(false);
		addColumn(genericColumn);
		
        genericColumn = new GenericColumn();
        genericColumn.setTitle("Timestamp");
        genericColumn.setIndex(1);
        genericColumn.setType(Date.class);
        genericColumn.setWidth(100);
        genericColumn.setEditable(true);
        addColumn(genericColumn);
		
		JobSiteColumn jobSiteColumn = new JobSiteColumn();
		jobSiteColumn.setTitle("Job Site");
		jobSiteColumn.setIndex(2);
		jobSiteColumn.setType(JobSiteDTO.class);
		jobSiteColumn.setWidth(150);
		jobSiteColumn.setEditable(false);
		jobSiteColumn.addJobSiteListListener(this);
		jobSiteColumn.loadList();
		addColumn(jobSiteColumn);
		
		AgencyColumn agencyColumn = new AgencyColumn();
		agencyColumn.setTitle("Agency");
		agencyColumn.setIndex(3);
		agencyColumn.setType(AgencyDTO.class);
		agencyColumn.setWidth(150);
		agencyColumn.setEditable(false);
		agencyColumn.addAgencyListListener(this);
		agencyColumn.loadList();
		addColumn(agencyColumn);
		
		AgentColumn agentColumn = new AgentColumn();
		agentColumn.setTitle("Agent");
		agentColumn.setIndex(4);
		agentColumn.setType(AgentDTO.class);
		agentColumn.setWidth(150);
		agentColumn.setEditable(false);
		agentColumn.addAgentListListener(this);
		agentColumn.loadList();
		addColumn(agentColumn);
		
		genericColumn = new GenericColumn();
		genericColumn.setTitle("Description");
		genericColumn.setIndex(5);
		genericColumn.setType(String.class);
		genericColumn.setWidth(200);
		genericColumn.setEditable(true);
		addColumn(genericColumn);
		
		genericColumn = new GenericColumn();
		genericColumn.setTitle("Job Code");
		genericColumn.setIndex(6);
		genericColumn.setType(String.class);
		genericColumn.setWidth(100);
		genericColumn.setEditable(true);
		addColumn(genericColumn);
		
	}
	public void setTable(AdvertisedJobTable table)	{
		this.table = table;
	}
	public void setJobSiteListLoaded(boolean loaded)	{
		this.jobSiteListLoaded = loaded;
		checkAllListsLoaded();
	}
	public void setAgencyListLoaded(boolean loaded)	{
		this.agencyListLoaded = loaded;
		checkAllListsLoaded();
	}
	public void setAgentListLoaded(boolean loaded)	{
		this.agentListLoaded = loaded;
		checkAllListsLoaded();
	}
	private void checkAllListsLoaded()	{
		if (jobSiteListLoaded && agencyListLoaded && agentListLoaded)	{
			table.listsLoaded();
		}
	}
}
